package com.example.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.home.entity.TabItem
import com.example.home.R

class TabAdapter(private val tabList: List<TabItem>, private val onTabClick: (Int) -> Unit) : RecyclerView.Adapter<TabAdapter.TabViewHolder>() {
    inner class TabViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tabTextView: TextView = itemView.findViewById(android.R.id.text1)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabViewHolder {
        // 使用自定义布局文件
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_string, parent, false)
        return TabViewHolder(view)
    }

    override fun onBindViewHolder(holder: TabViewHolder, position: Int) {
        val tabItem = tabList[position]
        holder.tabTextView.text = tabItem.title
        // 设置文字颜色，选中为红色，否则为黑色
        holder.tabTextView.setTextColor(
            if (tabItem.isSelected) holder.itemView.context.getColor(android.R.color.holo_red_light)
            else holder.itemView.context.getColor(android.R.color.black)
        )
        // 点击事件，回调给外部处理选中逻辑
        holder.itemView.setOnClickListener {
            onTabClick(position)
        }
    }

    override fun getItemCount(): Int = tabList.size
}
